package com.etoc.test.flow.kd;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.etoc.test.util.DateUtil;
import com.etoc.test.util.EncryptUtil;
import com.etoc.test.util.HttpUtil;

/**
 * �����Դ��ѯʵ����
 * <һ�仰���ܼ���>
 * <������ϸ����>
 * 
 * @author  �³�
 * @version  [�汾��, 2015-8-10]
 * @see  [�����/����]
 * @since  [��Ʒ/ģ��汾]
 */
public class FreeResourceInfo implements Serializable {
	/**
	 * ע������
	 */
	private static final long serialVersionUID = -3592583528888123982L;
	private String action;
	private String appKey;
	private String phoneNo;
	private String model;
	private String timeStamp;
	private String sign;
	private String appSecret;
	private String cycleId;
	Map<String, String> map = new TreeMap<String, String>();
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getCycleId() {
		return cycleId;
	}

	public void setCycleId(String cycleId) {
		this.cycleId = cycleId;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	
	public Map<String, String> getMap() {
		return map;
	}
	
	public String signString() {	
		if (this.action != null && !this.action.equals(""))
		{
			map.put("action", this.action);
		}
		if (this.appKey != null && !this.appKey.equals(""))
		{
			map.put("appKey", this.appKey);
		}
		if (this.cycleId != null && !this.cycleId.equals(""))
		{
			map.put("cycleId", this.cycleId);
		}
		if (this.model != null && !this.model.equals(""))
		{
			map.put("model", this.model);
		}
		if (this.phoneNo != null && !this.phoneNo.equals(""))
		{
			map.put("phoneNo", this.phoneNo);
		}
		if (this.timeStamp != null && !this.timeStamp.equals(""))
		{
			map.put("timeStamp", this.timeStamp);
		}
		
		Iterator<String> keySet = map.keySet().iterator();
		StringBuffer signString = new StringBuffer(this.appSecret);
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			signString.append(key).append(val);
		}
		signString.append(this.appSecret);
		return signString.toString(); 
	}

	@Override
	public String toString() {
		Iterator<String> keySet = map.keySet().iterator();
		String ret = "";
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			ret += key + "=" + val + "&";
		}
		ret += "sign" + "=" + sign;
		return ret;
	}
	
	public static void main(String[] args) throws Exception
	{
		FreeResourceInfo order = new FreeResourceInfo();
		order.setAction("freeResSer");
		order.setAppKey("4aea47a6465e3de901465e68c8100000");
		order.setAppSecret("unicom");
		order.setPhoneNo("18671326122");
		order.setCycleId("201509");
		order.setModel("0");
		order.setTimeStamp(DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss"));
		String sign = order.signString();
		order.setSign(EncryptUtil.encryptSHA(sign));
		
		String url = "http://113.57.243.18:8070/flowAgent";
		System.out.println(url + "?" + order.toString());
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Map<String, String> map = order.getMap();
		Iterator<String> keySet = map.keySet().iterator();
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			
			nvps.add(new BasicNameValuePair(key, val));
		}
		nvps.add(new BasicNameValuePair("sign", order.getSign()));
				
		long startTime = System.currentTimeMillis();
		String respCode = HttpUtil.httpRequest(url, nvps);
		long endTime = System.currentTimeMillis();
		System.out.println("freeResSer=" + respCode);
		System.out.println("spend " + (endTime - startTime) + " milli second.");
		
		/*String data = "535FCF911F6D3865C39849305B776DD9023E30FB1DA820250486C484F87AFC0A6281883618831E2EAB8A3708A3CC37502A7E2E6B712756749F2A96A0FE494216E44FC5B1C19CB9209CFF83D174E875FEA3EBB5050B1DA403CA9293FF23369D79D6995CEB70E44CEC81E4D07679242AAEA54722AA0B824369ECB7993C06C1B98E953438CCAC1E0086404A9996084A12F6047802DF269CF33D041D2C1E212871DE3B1B9A77473A92FF4D9525D52EC915FF194F6EF5DF2D39B685FFA547CAE7D676E3E4FF70734FB2B07E87A06B9C1B08B5F2298B31DE86E0C016A6C445E298AA4CFE5E05668246902C8C5A3E0FB0EB242ED85C89BC575F45ACAB0250EE321DEC764E964AAA4C40B8A5CEF5DA110FD9E80A76EB57BD7967886634B453CC4DCC31462CA196027DEFD7A44C33CDE186716D67DA2913BA1F8F1F4E403BDBD15D911F4E860F7E5A06E40655F1B4E07B89130D0712F85AFD48267AA73FCE4FBBB2F651BF2313A243463C56C45C42714D7EF6EF2C58AC469A8CEB9CD89642F53951E96923254CFC44020D58143B1B9A77473A92FF4D9525D52EC915FFCD2AA28829E4FB422C0B31135963E16A9CDA7121F0A0FC668911EB97293CC49EE331EAAD5B2002A4CD2AA28829E4FB421C88CF78A2BB87BB05792165245237FBAD8647012BF998921DB77F95FCA1BCA307626F4FBA16EAABF183BAE9D7486931FD6489D24DFA31512A7E2E6B712756749F2A96A0FE494216B91EC1DA89837D829492D1067B4E38160C66413F00229F3483B03BBAD258AF7844CAD0BB2EF032924A90A11BE349393061F8D0D89785DC0E3E82DCBBE8689C3D8B8DC22810B82D037EDC178856AE1AD1ABF0CB05B333CA0CB9C2BC0CDFA793A95D399CFB8D7347BA646C1DE5142B86565AC5D6168F8C576D92ABAE8B2B082E45D7395A8861C4D0B2E0E437DB08E13D22496E61F6D96BB0817C75A4E47518A0690B3E2F12223238B3E3E4FF70734FB2B04152A896E20F3B49E6E22926EAC6A9E3EE1192583A248CA9C1FC935887A6D62D7315CF9FE3B66C24596D1CFC52FDBFE669A1B5A7DF9A4CA55C7588DD08AB9C65D81CC10147AB8BF9F5B9D76D7AA857552046766C66B6E0CF559D5437C2A3E540F19AEC8CB840420102253666AA72A12E95488F1A6FB5D75FD6995CEB70E44CEC81E4D07679242AAE5612FFB46967C5B01788A4A0C3E3269A953438CCAC1E008636F57ABCF2046E008033F3AA13ABCC3576C86F0130CF1FEDD7395A8861C4D0B2E0E437DB08E13D22496E61F6D96BB081C17EB60C0A443AAB5AA554E6D7CA2BDB199269184EE3B454A9DEC46FAE49E3A68B54985584855E7BC17EB60C0A443AABF184215143CA3E010CBA6336C23542104B38A2BAFC1DF5D610DAB6CC5A1E427BCBC42F9EEBAC8D4B45ECD29036E615898143CC629F1DC739AA2E8FADF21C0EF73F9607DB23172594BF1B288B99C9F5F501FA0C14096FE5A8C6DDF4BEF3ADA61FDBAB34AFC705382ADA54823042DA69D294AEAC2B34C41CA8CAA20807CAE68D437D7DD4F9A92B60FD7140852D754A89B321CD5C42ECFA328CC59DA450A323E3A1637A72746D73A5AC5B2A949AD688DCCF6A7A44FD44F24257F05E169749A94EDD09956B202DECFBFDC3D6CFE2B4C12A6F7826F5773AC05318C3EAFEB966CFD145BD5C10C780A3803BC15C2D8C01BAD9FF9849A0F6531E550DF34A543EC435132FE3E4FF70734FB2B0FBA78CDD5497C0CADCEBC88CDD61371C141D2C0AB37F6A919790478627E0FBD1BC5C29981BE4BF5A";
		// cpPassword����С��8λ����Ĭ��ֵ12345678
		String result = EncryptUtil.decryptDES(data, "12345678", null);
		System.out.println(result);*/
	}
	
}
